package com.lk.springboot.controller;

import com.alibaba.fastjson.JSON;
import com.lk.springboot.bean.JsonResult;
import com.lk.springboot.bean.ResultCode;
import com.lk.springboot.bean.Users;
import com.lk.springboot.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;


/**
 * @author lk
 * @date 2020/1/7 0007 19:19
 * 无论什么类型的数据，都能直接扔到 JsonResult类里的data中（里面的data定义为泛型）
 * 最终的返回值始终为   String
 */

@RestController
public class JsonClassProjoController {
    @Autowired
    UsersMapper usersMapper;

    /*
     * 新增带参数的get请求与应答
     * */
    @RequestMapping(value = "/msg/{id}", method = RequestMethod.GET)
    public String getMessages(@PathVariable(value = "id") Integer id,
                              @RequestParam(value = "name", required = false, defaultValue = "nothing") String name) {
        JsonResult result = new JsonResult();
        result.setCode("200");
        result.setMsg(name);
        Users users = usersMapper.findById(id);
        result.setData(users);
        return JSON.toJSONString(result);
    }

    /*
    * 带参数的post请求，调用了存有状态码的枚举类，返回标准的json数据格式
    * */
    @RequestMapping(value = "/msg2", method = RequestMethod.POST)
    public String getMessages2(@RequestParam(value = "name") String name) {
        JsonResult result = new JsonResult(ResultCode.SUCCESS);
        ArrayList<String> list = new ArrayList<>();
        list.add(name);
        list.add("放了一堆string");
        list.add("可以是任意类型");
        list.add("本例是ArrayList<String>型");
        result.setData(list);
        return JSON.toJSONString(result);
    }

}
